﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;

namespace SwimmingCompetitions
{
    public static class SwimmingGlobals
    {
        [Serializable]
        public enum Mode
        {
            [Description("Спина")]
            Back,
            [Description("Вольный стиль")]
            Crawl,
            [Description("Брасс")]
            Brasse,
            [Description("Баттерфляй")]
            Butterfly,
            [Description("Комплекс")]
            Complex
        }

        [Serializable]
        public enum Participants
        {
            [Description("Мужчины")]
            Men = 0,
            [Description("Женщины")]
            Women = 1
        }

        public static string GetDescription<T>(this object enumerationValue)
            where T : struct
        {
            var type = enumerationValue.GetType();
            if (!type.IsEnum)
            {
                throw new ArgumentException("Enumeration value must be of Enum type", "enumerationValue");
            }

            //Tries to find a DescriptionAttribute for a potential friendly name
            //for the enum
            var memberInfo = type.GetMember(enumerationValue.ToString());
            if (memberInfo.Length > 0)
            {
                var attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (attrs.Length > 0)
                {
                    //Pull out the description value
                    return ((DescriptionAttribute)attrs[0]).Description;
                }
            }
            //If we have no description attribute, just return the ToString of the enum
            return enumerationValue.ToString();
        }
        
        private static int getMs(string s)
        {
            var value = Convert.ToSingle("0." + s, CultureInfo.InvariantCulture);
            return (int)Math.Round(value * 1000);//3 digits max
        }

        public static TimeSpan? ConvertToTimeSpan(string s)
        {
            try
            {
                if (s.IndexOf(':') < 0) //just number: parsed as seconds
                {
                    if (s.IndexOf('.') < 0) //no ms
                        return new TimeSpan(0, 0, 0, Int32.Parse(s));
                    var frags = s.Split('.');
                    return new TimeSpan(0, 0, 0, Int32.Parse(frags[0]), getMs(frags[1]));
                }
                var parts = s.Split(':');
                if (parts.Length != 2)
                {
                    return null;
                }
                else
                {
                    //parse as minutes and seconds
                    if (s.IndexOf('.') < 0) //no ms
                        return new TimeSpan(0, 0, Int32.Parse(parts[0]), Int32.Parse(parts[1]));
                    var frags = parts[1].Split('.');
                    return new TimeSpan(0, 0, Int32.Parse(parts[0]), Int32.Parse(frags[0]), getMs(frags[1]));
                }
            }
            catch
            {
                return null;
            }
        }

    }
}
